<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>MutationObserver 使用</title>
    <style>
      .container {
        background-color: red;
        width: 300px;
        height: 300px;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 10px;
        /* transition: all .5s; */
      }
    </style>
  </head>
  <body>
    <h1>通过 MutationObserver 类，监听元素属性的变化</h1>
    <div class="container" id="div">
      <h1 id="h1">打扫打扫打扫的</h1>
    </div>

    <button id="btn">更新</button>
  </body>
  <script>
    const $div = document.querySelector('#div')
    let i = 0
    document.querySelector('#btn').onclick = function () {
      $div.innerHTML = i++
      // document.querySelector('#h1').innerHTML = i++
    }

    const o = new MutationObserver((a) => {
      console.log(a)
    })
    const observerOptions = {
      childList: true, // 观察目标子节点的变化，是否有添加或者删除
      attributes: true, // 观察属性变动
      subtree: true // 观察后代节点，默认为 false
    }
    o.observe($div, observerOptions)
  </script>
</html>
